package com.sright.bcard.mini.web.controller;

import com.sright.bcard.content.share.request.CardParamRequest;
import com.sright.bcard.content.share.service.CardService;
import com.sright.bcard.common.service.share.dto.enterprise.IndustryDTO;
import com.sright.bcard.enterprise.share.service.IndustryService;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 名片相关controller
 */
@RestController
public class PersonalCardController {

    @Autowired
    private CardService cardService;

    @Autowired
    private IndustryService industryService;

    /**
     * 创建个人名片
     * @return
     */
    @PostMapping(WebUrl.PersonalCard)
    public SrightResponse addCard(@RequestBody IndustryDTO industryDTO,@RequestAttribute TokenParam tokenParam) {
        buildIndustryUserContext(industryDTO,tokenParam);
        return cardService.addPersonalCard(industryDTO);
    }

    /**
     * 个人名片编辑 - 中文
     * @return
     */
    @PutMapping(WebUrl.PersonalCardZh)
    public SrightResponse cardZh(@RequestBody CardParamRequest cardParamRequest,@RequestAttribute TokenParam tokenParam) {
        buildCardUserContext(cardParamRequest,tokenParam);
        return cardService.cardZh(cardParamRequest);
    }

    /**
     * 个人名片编辑 - 英文
     */
    @PutMapping(WebUrl.PersonalCardEn)
    public SrightResponse cardEn(@RequestBody CardParamRequest cardParamRequest,@RequestAttribute TokenParam tokenParam) {
        buildCardUserContext(cardParamRequest,tokenParam);
        return cardService.cardEn(cardParamRequest);
    }

    /**
     * 个人名片编辑文字颜色
     * @param cardParamRequest
     * @param tokenParam
     * @return
     */
    @PutMapping(WebUrl.EditPersonalColor)
    public SrightResponse editColor(@RequestBody CardParamRequest cardParamRequest, @RequestAttribute TokenParam tokenParam) {
        return cardService.editFontColor(cardParamRequest.getColor(), tokenParam.getPersonalUserId());
    }

    @GetMapping(WebUrl.IndustryList)
    public SrightResponse industryList() {
        return industryService.personalIndustryList();
    }

    private IndustryDTO buildIndustryUserContext(IndustryDTO industryDTO, TokenParam tokenParam) {
        industryDTO.setMemberId(tokenParam.getMemberId());
        industryDTO.setEnterpriseId(tokenParam.getEnterpriseId());
        return industryDTO;
    }

    private CardParamRequest buildCardUserContext(CardParamRequest cardParamRequest, TokenParam tokenParam) {
        cardParamRequest.setMemberId(tokenParam.getMemberId());
        cardParamRequest.setUserId(tokenParam.getPersonalUserId());
        cardParamRequest.setEnterpriseId(tokenParam.getEnterpriseId());
        return cardParamRequest;
    }
}
